import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

/*
General class to be used for basic methods that need to be implemented by any class that needs to be drawn
	-Has events for listening for keys
	-Has events for listening for mouse events, primarily for mouse clicks. Mouse movement will be used only in classes that need it.
	since the majority of the mouse action will be clicks only.
	-Also contains two other methods, with more to be possibly implemented. Notes on those below. 
*/
public abstract class Superclass extends JFrame implements KeyListener, MouseListener, MouseWheelListener
{
	/* These variables are for various purposes. The path is used for the extractImage methods
	 and the xCoord and yCoord right now are used for the location on the image that the extractImage
	 needs. The Dimension imgSize is used for the actual dimension of the total image and is used for
	 various other methods in the subclasses.
	 */
	
	/* Note: The methods need to be modified at a later point to account for the (y + height) to not go outside the actual size of the image
	 	and through a RasterImage error
	 */
	
	public String path;
	public int xCoord;
	public int yCoord;
	public int imgExtractSize[]; //the size of the image that is being extracted (0) is width (1) is height
	public int totalImgSize[]; //the total size of the image (0) is width and (1) is height
	public static double scaleFactor;
	
	//initiates all of the objects in the class so null pointers are not thrown
	public Superclass()
	{
		imgExtractSize = new int[2];
		totalImgSize = new int[2];
		scaleFactor = 1;
	}
	
	/*List of all of the void methods for KeyListener and MouseListener */
	public void mouseClicked(MouseEvent arg0) {}
	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}
	@Override
	public void keyPressed(KeyEvent arg0) {}
	@Override
	public void keyReleased(KeyEvent arg0) {}
	@Override
	public void keyTyped(KeyEvent arg0) {}
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) 
	{}
		/* This method returns an BufferedImage of the image that is desired to be drawn onto the JFrame. It will be used for all
	 of the classes that require a image of themselves.
	 
	 Note: Currently only used for background, which has the lowest priority of being drawn (i.e. it is drawn first)
	 */
	public BufferedImage  extractImage()
	{
		BufferedImage i = null;
		ImageIcon returnImg =  null;
		
		i = baseImage();
		BufferedImage cut;
		
		//Need to fix so that it accounts for the scaling. When it hits a certain point it trys to grab out of the actual image
		if (imgExtractSize[0] < i.getWidth() && imgExtractSize[1]  < i.getHeight())
		{
			if (xCoord + imgExtractSize[0] >= i.getWidth())
			{
				do
					xCoord--;
				while(xCoord + imgExtractSize[0] > i.getWidth() || xCoord == 0);
			}
		
			if (yCoord + imgExtractSize[1] >= i.getHeight())
			{
				do
					yCoord--;
				while(yCoord + imgExtractSize[1] > i.getHeight() || yCoord == 0);
			}
			cut = i.getSubimage(xCoord, yCoord, imgExtractSize[0], imgExtractSize[1]);
		}
		else
			cut = i;
		
		//returnImg = new ImageIcon(cut);
		
		return cut;
	}
	
	/* This method returns a BufferedImage of the image loaded from the path. It is used for extraction purposes in the extractImage 
	 method and also is used by certain other methods for the dimensions of the original image.
	 */
	public BufferedImage baseImage()
	{	
		BufferedImage base = null;
		String path1 = "test image.jpg";
		
		try
		{
			base = ImageIO.read(new File(path1));
		}
		catch  (IOException ex)
		{}
		base = resizeImage(base, base.TYPE_INT_ARGB);
		totalImgSize[0] = base.getWidth();
		totalImgSize[1] = base.getHeight();
		
		return base;
	}
	
	/* This method returns a resized image of the initial image sent to it that is determined by the scaleFactor variable
		
		In essence,  this ccreates a new BufferedImage of the original's height and width multiplied by the current scale 
		factor, which is default one.
		
		It then uses Graphics to draw the original image on that new image
	*/
	public static BufferedImage resizeImage(BufferedImage originalImage, int type)
	{
		int width = (int)(originalImage.getWidth() * scaleFactor);
		int height = (int)(originalImage.getHeight() * scaleFactor);
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();
			 
		return resizedImage;
	}  
}
